package com.xiaomi.front.controller;

import com.xiaomi.front.service.mycollection.MycollectionService;
import com.xiaomi.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class CollectionController {

    @Autowired
    private MycollectionService mycollectionService;

    //ajax 是否收藏
    @RequestMapping(value = "/Comm_coll/{deid}", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object ifCollection(@PathVariable("deid") int commid, HttpSession session) {
        //1未登录 2 收藏 true 3未查到 false
        User loginUser = (User) session.getAttribute("user");
        if (loginUser == null) {
            return "未登录";
        } else {
            if (mycollectionService.ifCollection(loginUser.getUid(), commid)) {
                return "已收藏";
            } else {
                return "未收藏";
            }
        }
    }

    //添加/取消收藏
    @RequestMapping(value = "/addComm_coll/{deid}")
    @ResponseBody
    public Object AddCollection(@PathVariable("deid") int commid, HttpSession session) {
        //1未登录 2 true 3未查到 false
        User loginUser = (User) session.getAttribute("user");
        if (loginUser == null) {
            return "nologin";
        } else {
            if (mycollectionService.ifCollection(loginUser.getUid(), commid)) {
                int count = mycollectionService.deleteCollection(loginUser.getUid(), commid);
                return "fail";
            } else {
                int count = mycollectionService.addCollecion(loginUser.getUid(), commid);
                return "success";
            }
        }
    }
}
